package com.fr.base.core.image4j.codec.ico;

import com.fr.base.core.image4j.codec.bmp.BMPEncoder;
import com.fr.base.core.image4j.codec.bmp.InfoHeader;
import com.fr.base.core.image4j.io.LittleEndianOutputStream;
import com.fr.base.core.image4j.util.ConvertUtil;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

public class ICOEncoder
{
  public static void write(BufferedImage paramBufferedImage, File paramFile)
    throws IOException
  {
    write(paramBufferedImage, -1, paramFile);
  }

  public static void write(BufferedImage paramBufferedImage, OutputStream paramOutputStream)
    throws IOException
  {
    write(paramBufferedImage, -1, paramOutputStream);
  }

  public static void write(List paramList, OutputStream paramOutputStream)
    throws IOException
  {
    write(paramList, null, null, paramOutputStream);
  }

  public static void write(List paramList, File paramFile)
    throws IOException
  {
    write(paramList, null, paramFile);
  }

  public static void write(List paramList, int[] paramArrayOfInt, File paramFile)
    throws IOException
  {
    write(paramList, paramArrayOfInt, new FileOutputStream(paramFile));
  }

  public static void write(List paramList, int[] paramArrayOfInt, boolean[] paramArrayOfBoolean, File paramFile)
    throws IOException
  {
    write(paramList, paramArrayOfInt, paramArrayOfBoolean, new FileOutputStream(paramFile));
  }

  public static void write(BufferedImage paramBufferedImage, int paramInt, File paramFile)
    throws IOException
  {
    write(paramBufferedImage, paramInt, new FileOutputStream(paramFile));
  }

  public static void write(BufferedImage paramBufferedImage, int paramInt, OutputStream paramOutputStream)
    throws IOException
  {
    ArrayList localArrayList = new ArrayList(1);
    localArrayList.add(paramBufferedImage);
    write(localArrayList, new int[] { paramInt }, new boolean[] { false }, paramOutputStream);
  }

  public static void write(List paramList, int[] paramArrayOfInt, OutputStream paramOutputStream)
    throws IOException
  {
    write(paramList, paramArrayOfInt, null, paramOutputStream);
  }

  public static void write(List paramList, int[] paramArrayOfInt, boolean[] paramArrayOfBoolean, OutputStream paramOutputStream)
    throws IOException
  {
    BufferedImage localBufferedImage1;
    Object localObject1;
    Object localObject2;
    LittleEndianOutputStream localLittleEndianOutputStream = new LittleEndianOutputStream(paramOutputStream);
    int i = paramList.size();
    writeFileHeader(i, 1, localLittleEndianOutputStream);
    int j = 6 + i * 16;
    ArrayList localArrayList1 = new ArrayList(i);
    ArrayList localArrayList2 = new ArrayList(i);
    ArrayList localArrayList3 = null;
    if (paramArrayOfBoolean != null)
      localArrayList3 = new ArrayList(i);
    ImageWriter localImageWriter = null;
    for (int k = 0; k < i; ++k)
    {
      localBufferedImage1 = (BufferedImage)paramList.get(k);
      int l = (paramArrayOfInt == null) ? -1 : paramArrayOfInt[k];
      localObject1 = (l == -1) ? localBufferedImage1 : convert(localBufferedImage1, l);
      localArrayList2.add(localObject1);
      localObject2 = BMPEncoder.createInfoHeader((BufferedImage)localObject1);
      IconEntry localIconEntry = createIconEntry((InfoHeader)localObject2);
      if (paramArrayOfBoolean != null)
        if (paramArrayOfBoolean[k] != 0)
        {
          if (localImageWriter == null)
            localImageWriter = getPNGImageWriter();
          byte[] arrayOfByte = encodePNG(localImageWriter, (BufferedImage)localObject1);
          localArrayList3.add(arrayOfByte);
          localIconEntry.iSizeInBytes = arrayOfByte.length;
        }
        else
        {
          localArrayList3.add(null);
        }
      localObject2.iHeight *= 2;
      localIconEntry.iFileOffset = j;
      j += localIconEntry.iSizeInBytes;
      localIconEntry.write(localLittleEndianOutputStream);
      localArrayList1.add(localObject2);
    }
    for (k = 0; k < i; ++k)
    {
      localBufferedImage1 = (BufferedImage)paramList.get(k);
      BufferedImage localBufferedImage2 = (BufferedImage)localArrayList2.get(k);
      if ((paramArrayOfBoolean == null) || (paramArrayOfBoolean[k] == 0))
      {
        localObject1 = (InfoHeader)localArrayList1.get(k);
        ((InfoHeader)localObject1).write(localLittleEndianOutputStream);
        if (((InfoHeader)localObject1).sBitCount <= 8)
        {
          localObject2 = (IndexColorModel)localBufferedImage2.getColorModel();
          BMPEncoder.writeColorMap((IndexColorModel)localObject2, localLittleEndianOutputStream);
        }
        writeXorBitmap(localBufferedImage2, (InfoHeader)localObject1, localLittleEndianOutputStream);
        writeAndBitmap(localBufferedImage1, localLittleEndianOutputStream);
      }
      else
      {
        localObject1 = (byte[])localArrayList3.get(k);
        localLittleEndianOutputStream.write(localObject1);
      }
    }
  }

  public static void writeFileHeader(int paramInt1, int paramInt2, LittleEndianOutputStream paramLittleEndianOutputStream)
    throws IOException
  {
    paramLittleEndianOutputStream.writeShortLE(0);
    paramLittleEndianOutputStream.writeShortLE((short)paramInt2);
    paramLittleEndianOutputStream.writeShortLE((short)paramInt1);
  }

  public static IconEntry createIconEntry(InfoHeader paramInfoHeader)
  {
    IconEntry localIconEntry = new IconEntry();
    localIconEntry.bWidth = ((paramInfoHeader.iWidth == 256) ? 0 : paramInfoHeader.iWidth);
    localIconEntry.bHeight = ((paramInfoHeader.iHeight == 256) ? 0 : paramInfoHeader.iHeight);
    localIconEntry.bColorCount = ((paramInfoHeader.iNumColors >= 256) ? 0 : paramInfoHeader.iNumColors);
    localIconEntry.bReserved = 0;
    localIconEntry.sPlanes = 1;
    localIconEntry.sBitCount = paramInfoHeader.sBitCount;
    int i = BMPEncoder.getColorMapSize(paramInfoHeader.sBitCount);
    int j = BMPEncoder.getBitmapSize(paramInfoHeader.iWidth, paramInfoHeader.iHeight, paramInfoHeader.sBitCount);
    int k = BMPEncoder.getBitmapSize(paramInfoHeader.iWidth, paramInfoHeader.iHeight, 1);
    int l = paramInfoHeader.iSize + i + j + k;
    localIconEntry.iSizeInBytes = l;
    localIconEntry.iFileOffset = 0;
    return localIconEntry;
  }

  public static void writeAndBitmap(BufferedImage paramBufferedImage, LittleEndianOutputStream paramLittleEndianOutputStream)
    throws IOException
  {
    int i2;
    int i3;
    int i4;
    int i5;
    WritableRaster localWritableRaster1 = paramBufferedImage.getAlphaRaster();
    if ((paramBufferedImage.getColorModel() instanceof IndexColorModel) && (paramBufferedImage.getColorModel().hasAlpha()))
    {
      i = paramBufferedImage.getWidth();
      j = paramBufferedImage.getHeight();
      k = BMPEncoder.getBytesPerLine1(i);
      arrayOfByte = new byte[k];
      IndexColorModel localIndexColorModel = (IndexColorModel)paramBufferedImage.getColorModel();
      WritableRaster localWritableRaster2 = paramBufferedImage.getRaster();
      i2 = j - 1;
      while (true)
      {
        if (i2 < 0)
          return;
        for (i3 = 0; i3 < i; ++i3)
        {
          i4 = i3 / 8;
          i5 = i3 % 8;
          int i6 = localWritableRaster2.getSample(i3, i2, 0);
          int i7 = localIndexColorModel.getAlpha(i6);
          int i8 = (i7 ^ 0xFFFFFFFF) & 0x1;
          arrayOfByte[i4] = setBit(arrayOfByte[i4], i5, i8);
        }
        paramLittleEndianOutputStream.write(arrayOfByte);
        --i2;
      }
    }
    if (localWritableRaster1 == null)
    {
      i = paramBufferedImage.getHeight();
      j = paramBufferedImage.getWidth();
      k = BMPEncoder.getBytesPerLine1(j);
      arrayOfByte = new byte[k];
      for (l = 0; l < k; ++l)
        arrayOfByte[l] = 0;
      l = i - 1;
      while (true)
      {
        if (l < 0)
          return;
        paramLittleEndianOutputStream.write(arrayOfByte);
        --l;
      }
    }
    int i = paramBufferedImage.getWidth();
    int j = paramBufferedImage.getHeight();
    int k = BMPEncoder.getBytesPerLine1(i);
    byte[] arrayOfByte = new byte[k];
    for (int l = j - 1; l >= 0; --l)
    {
      for (int i1 = 0; i1 < i; ++i1)
      {
        i2 = i1 / 8;
        i3 = i1 % 8;
        i4 = localWritableRaster1.getSample(i1, l, 0);
        i5 = (i4 ^ 0xFFFFFFFF) & 0x1;
        arrayOfByte[i2] = setBit(arrayOfByte[i2], i3, i5);
      }
      paramLittleEndianOutputStream.write(arrayOfByte);
    }
  }

  private static byte setBit(byte paramByte, int paramInt1, int paramInt2)
  {
    int i = 1 << 7 - paramInt1;
    paramByte = (byte)(paramByte & (i ^ 0xFFFFFFFF));
    paramByte = (byte)(paramByte | paramInt2 << 7 - paramInt1);
    return paramByte;
  }

  private static void writeXorBitmap(BufferedImage paramBufferedImage, InfoHeader paramInfoHeader, LittleEndianOutputStream paramLittleEndianOutputStream)
    throws IOException
  {
    WritableRaster localWritableRaster1 = paramBufferedImage.getRaster();
    switch (paramInfoHeader.sBitCount)
    {
    case 1:
      BMPEncoder.write1(localWritableRaster1, paramLittleEndianOutputStream);
      break;
    case 4:
      BMPEncoder.write4(localWritableRaster1, paramLittleEndianOutputStream);
      break;
    case 8:
      BMPEncoder.write8(localWritableRaster1, paramLittleEndianOutputStream);
      break;
    case 24:
      BMPEncoder.write24(localWritableRaster1, paramLittleEndianOutputStream);
      break;
    case 32:
      WritableRaster localWritableRaster2 = paramBufferedImage.getAlphaRaster();
      BMPEncoder.write32(localWritableRaster1, localWritableRaster2, paramLittleEndianOutputStream);
    }
  }

  public static BufferedImage convert(BufferedImage paramBufferedImage, int paramInt)
  {
    BufferedImage localBufferedImage = null;
    switch (paramInt)
    {
    case 1:
      localBufferedImage = ConvertUtil.convert1(paramBufferedImage);
      break;
    case 4:
      localBufferedImage = ConvertUtil.convert4(paramBufferedImage);
      break;
    case 8:
      localBufferedImage = ConvertUtil.convert8(paramBufferedImage);
      break;
    case 24:
      int i = paramBufferedImage.getColorModel().getPixelSize();
      if ((i == 24) || (i == 32))
      {
        localBufferedImage = paramBufferedImage;
        break label142:
      }
      localBufferedImage = ConvertUtil.convert24(paramBufferedImage);
      break;
    case 32:
      int j = paramBufferedImage.getColorModel().getPixelSize();
      if ((j == 24) || (j == 32))
      {
        localBufferedImage = paramBufferedImage;
        break label142:
      }
      localBufferedImage = ConvertUtil.convert32(paramBufferedImage);
    }
    label142: return localBufferedImage;
  }

  private static ImageWriter getPNGImageWriter()
  {
    ImageWriter localImageWriter = null;
    Iterator localIterator = ImageIO.getImageWritersByFormatName("png");
    if (localIterator.hasNext())
      localImageWriter = (ImageWriter)localIterator.next();
    return localImageWriter;
  }

  private static byte[] encodePNG(ImageWriter paramImageWriter, BufferedImage paramBufferedImage)
    throws IOException
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    ImageOutputStream localImageOutputStream = ImageIO.createImageOutputStream(localByteArrayOutputStream);
    paramImageWriter.setOutput(localImageOutputStream);
    paramImageWriter.write(paramBufferedImage);
    localByteArrayOutputStream.flush();
    byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
    return arrayOfByte;
  }
}